/*  
  -- The Lime II Project -- 

  A tuplespaces-based middleware for coordinating agents and mobile hosts.
  Copyright (C) 2005.
  Gian Pietro Picco, Amy L. Murphy, Lorenzo Bellini.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/
 
package testing;
import lime2.*; 
import lights.adapters.*;
import lights.interfaces.*;
import java.util.Random;

/**
 * A simple agent performing outg of nine tuples: first three have the local host as destination; user can select
 * destination of the other six (we suppose that they are divided into two group of three tuples each).
 * 
 * @author Gian Pietro Picco
 * @author Amy Murphy
 * @author Lorenzo Bellini
 */

public class Outg implements Runnable 
{
 //	identification number	
 private int id = new Random().nextInt(1000);		
	
 // private data members
 private Location dest1 = Location.HERE;
 private Location dest2 = Location.HERE;
 
 // String value for "here" 
 private final String HereS = "here";
 
 /**
  * Constructor method (default). Destination locations are here.
  */
 public Outg()
 {
  // empty
 }
 
 /**
  * Constructor method.
  * 
  * @param a [name/IP of destination location]
  * @param b [port of destination location]
  */
 public Outg(String a, String b)
 {
  dest1 = dest2 = Location.build(a, new Integer(b).intValue());
 }
 
 /**
  * Constructor method.
  * 
  * @param a [name/IP of first destination location]
  * @param b [port of first destination location]
  * @param c [name/IP of second destination location]
  * @param d [port of second destination location]
  */
 public Outg(String a, String b, String c, String d)
 {
  dest1 = Location.build(a, new Integer(b).intValue());
  dest2 = Location.build(c, new Integer(d).intValue());
 }
 
 /**
  * Run method.
  */
 public void run()
 {
  System.out.println("Outg " + id + ": started");     
  LimeTupleSpace lts = LimeTupleSpace.get("testing"); 
  
  ITuple[] tuples = new ITuple[] { 
  	                              new Tuple().add(new Field().setValue("hello0")), 
  	                              new Tuple().add(new Field().setValue("hello1")),
  	                              new Tuple().add(new Field().setValue("hello2")),
                                  new Tuple().add(new Field().setValue("hello3")),
								  new Tuple().add(new Field().setValue("hello4")), 
  	                              new Tuple().add(new Field().setValue("hello5")),
  	                              new Tuple().add(new Field().setValue("hello6")),
                                  new Tuple().add(new Field().setValue("hello7")),
								  new Tuple().add(new Field().setValue("hello8"))
                                 }; 
  
  ITuple[][] group = new ITuple[][] {
                                     new ITuple[] { tuples[0], tuples[1], tuples[2] },
                                     new ITuple[] { tuples[3], tuples[4], tuples[5] },
                                     new ITuple[] { tuples[6], tuples[7], tuples[8] }
                                    }; 
  
  Location[] whereto = new Location[] { Location.HERE, dest1, dest2 };          
             
  LimeTuple[] lt = lts.outg(whereto, group);                               
  System.out.println("Outg " + id + ": performed outg of tuples");               
  System.out.println("Outg " + id + ": dies");
 }
 
 // private method for formatting textual output: generates an empty string of lenght n (n > 1)
 private String emptyString(int n)
 {
  String ret = " ";	
  for (int i = 1; i < n; i++) ret = " ".concat(ret);   	
  return ret;
 }
 
}
